import redisConnect from './redisConnection';
import config from '../../config';
import { RedisConfigType, RedisClientTypes, RedisToolType } from '../../types/redis';

const redisConfig: RedisConfigType = {
    socket: {
        host: config.DATABASE.REDIS.HOST,
        port: 6379,
    },
};

class RedisTool implements RedisToolType {
    redisClient: RedisClientTypes | null;

    config: RedisConfigType;

    constructor(options?: unknown) {
        this.redisClient = null;
        if (options) {
            this.config = Object.assign(redisConfig, options);
        } else {
            this.config = redisConfig;
        }
        this.connToRedis();
    }

    async connToRedis(): Promise<null> {
        if (this.redisClient) {
            // 已创建
            return null;
        }
        try {
            this.redisClient = await redisConnect(this.config);
        } catch (err) {
            // eslint-disable-next-line no-console
            console.log(`Redis connect Error ${err}`);
        }
        return null;
    }

    // 存储 string 类型的 key-value
    async setString(key: string, value: any, expire = 300): Promise<string | null> {
        const val: string = typeof value !== 'string' ? JSON.stringify(value) : value;
        try {
            if (this.redisClient == null) {
                throw new Error('this.redisClient undefined!');
            }
            const res = await this.redisClient.set(key, val);
            this.redisClient.expire(key, expire);
            return res;
        } catch (err) {
            console.error(`Redis setString Error ${err}`);
        }
        return null;
    }

    // 获取 string 类型的 key-value
    async getString(key: string): Promise<string | null> {
        const id: string = typeof key !== 'string' ? JSON.stringify(key) : key;
        try {
            if (this.redisClient == null) {
                throw new Error('this.redisClient undefined!');
            }
            const res = await this.redisClient.get(id);
            return res;
        } catch (err) {
            console.error(`Redis getString Error ${err}`);
        }
        return null;
    }

    // 删除 string 类型中的 key-value
    async delString(key: string): Promise<number | null> {
        try {
            if (this.redisClient == null) {
                throw new Error('this.redisClient undefined!');
            }
            const res = await this.redisClient.del(key);
            return res;
        } catch (err) {
            console.error(`Redis delString Error ${err}`);
        }
        return null;
    }

    // 获取当前 redisClient 数据库中 key 的数量
    async getDbSize(): Promise<number | null> {
        try {
            if (this.redisClient == null) {
                throw new Error('this.redisClient undefined!');
            }
            const res = await this.redisClient.dbSize();
            return res;
        } catch (err) {
            console.error(`Redis getDbSize Error ${err}`);
        }
        return null;
    }
}

// eslint-disable-next-line import/prefer-default-export
export const redisUser = new RedisTool({ db: 1 });

// export default new RedisTool();
